package com.qf.lja.service;

import com.qf.lja.dao.OrderDao;
import com.qf.lja.enity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet(urlPatterns = "/orderShow")
public class OrderShowService extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //utf-8
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");

        String statusFilter = req.getParameter("statusFilter");
        OrderDao orderDao = new OrderDao();


        if (statusFilter == null) {
            req.setAttribute("orderList", orderDao.findOrderList(user.getUserId()));
            req.getRequestDispatcher("orderList.jsp").forward(req, resp);
        }
        if (user == null) {
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }

        if ("all".equals(statusFilter)) {
            req.setAttribute("orderList", orderDao.findOrderList(user.getUserId()));
        } else {
            req.setAttribute("orderList", orderDao.findOrderByIdAndStatu(user.getUserId(), statusFilter));
        }
        req.getRequestDispatcher("orderList.jsp").forward(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //utf-8
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        String statusFilter = req.getParameter("statusFilter");
        OrderDao orderDao = new OrderDao();


        if (statusFilter == null) {
            req.setAttribute("orderList", orderDao.findOrderList(user.getUserId()));
            req.getRequestDispatcher("orderList.jsp").forward(req, resp);
        }

        System.out.println("status : " + statusFilter);
        if (user == null) {
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }

        if ("all".equals(statusFilter)) {
            req.setAttribute("orderList", orderDao.findOrderList(user.getUserId()));
        } else {
            req.setAttribute("orderList", orderDao.findOrderByIdAndStatu(user.getUserId(), statusFilter));
        }
        req.getRequestDispatcher("orderList.jsp").forward(req, resp);
    }
}
